<html>
<head><meta charset="utf-8"><title>mutable borrows in constants · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html">mutable borrows in constants</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199276735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199276735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199276735">(May 30 2020 at 21:29)</a>:</h4>
<p>I've started working on <a href="https://github.com/rust-lang/rust/issues/71212">https://github.com/rust-lang/rust/issues/71212</a> and following <span class="user-mention" data-user-id="124288">@oli</span> 's instructions I changed <a href="https://github.com/rust-lang/rust/blob/77f95a89a108d4cd6ad1230fb210b3640952b146/src/librustc_mir/transform/check_consts/qualifs.rs#L81">https://github.com/rust-lang/rust/blob/77f95a89a108d4cd6ad1230fb210b3640952b146/src/librustc_mir/transform/check_consts/qualifs.rs#L81</a> to also match mutable references. However we are not sure if that's enough. <span class="user-mention" data-user-id="118594">@ecstatic-morse</span> do you have any opinions/suggestions about it?</p>



<a name="199524876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199524876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199524876">(Jun 02 2020 at 17:21)</a>:</h4>
<p>We use <code>HasMutInterior</code> to forbid <code>&amp;x</code> when <code>x</code> is something with interior mutability like a <code>Cell</code>. Won't you be forbidding <code>&amp;&amp;mut x</code> everywhere with that change?</p>



<a name="199559173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199559173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199559173">(Jun 02 2020 at 21:56)</a>:</h4>
<p>I did the following. I hope I understood what you meant</p>



<a name="199559227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199559227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199559227">(Jun 02 2020 at 21:57)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">FOO</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">5</span><span class="p">);</span><span class="w"> </span><span class="c1">// Is OK in my branch and in master</span>
<span class="k">const</span><span class="w"> </span><span class="n">BAR</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="o">**</span><span class="p">(</span><span class="o">&amp;&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">5</span><span class="p">);</span><span class="w"> </span><span class="c1">// Is OK in my branch and in master</span>
<span class="k">const</span><span class="w"> </span><span class="n">BAZ</span>: <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w">  </span><span class="c1">// Is a compilation error in my branch and an ICE master</span>
</code></pre></div>



<a name="199562226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199562226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199562226">(Jun 02 2020 at 22:38)</a>:</h4>
<p>That's correct, but I'm more interested in the how than the fact that it works. I'm happy to review a PR. I don't have a concrete strategy in mind for how to fix <a href="https://github.com/rust-lang/rust/issues/71212">#71212</a>.</p>



<a name="199563625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199563625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199563625">(Jun 02 2020 at 23:00)</a>:</h4>
<p>sure I'll open a draft pr and ping you</p>



<a name="199585706"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199585706" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199585706">(Jun 03 2020 at 07:07)</a>:</h4>
<p>I think the question was about <code>const BIZ: &amp;'static &amp;'static mut usize = &amp;&amp;mut 5;</code>, which isn't mutable but the changes we discussed will mark it as such</p>



<a name="199585745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199585745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199585745">(Jun 03 2020 at 07:08)</a>:</h4>
<p>then again, this may be fine for a first version</p>



<a name="199624731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199624731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199624731">(Jun 03 2020 at 14:15)</a>:</h4>
<div class="codehilite"><pre><span></span><code>error: mutable memory (`&amp;mut`) is not allowed in constant
 --&gt; asd.rs:7:1
  |
7 | const BIZ: &amp;&#39;static &amp;&#39;static mut usize = &amp;&amp;mut 5;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
</code></pre></div>


<p>both of you were right.</p>



<a name="199627769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199627769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199627769">(Jun 03 2020 at 14:39)</a>:</h4>
<p>That error is not from const checking though?</p>



<a name="199627803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199627803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199627803">(Jun 03 2020 at 14:39)</a>:</h4>
<p>Should this be an AST based qualif?</p>



<a name="199627889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199627889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199627889">(Jun 03 2020 at 14:40)</a>:</h4>
<blockquote>
<p>Should this be an AST based qualif?</p>
</blockquote>



<a name="199627916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199627916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199627916">(Jun 03 2020 at 14:40)</a>:</h4>
<p>Im not familiar with that term</p>



<a name="199628024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628024">(Jun 03 2020 at 14:41)</a>:</h4>
<p>What would that mean?</p>



<a name="199628128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628128">(Jun 03 2020 at 14:42)</a>:</h4>
<p>(deleted)</p>



<a name="199628140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628140">(Jun 03 2020 at 14:42)</a>:</h4>
<p>(deleted)</p>



<a name="199628141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628141">(Jun 03 2020 at 14:42)</a>:</h4>
<p>(deleted)</p>



<a name="199628142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628142">(Jun 03 2020 at 14:42)</a>:</h4>
<p>(deleted)</p>



<a name="199628146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628146">(Jun 03 2020 at 14:42)</a>:</h4>
<p>nevermind</p>



<a name="199628254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628254">(Jun 03 2020 at 14:43)</a>:</h4>
<p>I was wondering if there was a way to know if a constant is exposed to be used during runtime or not</p>



<a name="199628395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628395">(Jun 03 2020 at 14:44)</a>:</h4>
<p>If you were to do that with HasMutInterior, you would disallow <code>&amp;&amp;mut</code> anywhere in a const fn, not just the final value.</p>



<a name="199628480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628480">(Jun 03 2020 at 14:45)</a>:</h4>
<p>I think this needs more of a "top-down" approach, changing a single line here or there is not enough</p>



<a name="199628608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628608">(Jun 03 2020 at 14:46)</a>:</h4>
<p>You could write a new Qualif for mutable references and only check for it in the final value of a const.</p>



<a name="199628726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628726">(Jun 03 2020 at 14:47)</a>:</h4>
<p>Ohhh ok</p>



<a name="199628764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628764">(Jun 03 2020 at 14:47)</a>:</h4>
<p>Did you consider my proposal to disallow &amp;mut at the top level of a const or static?</p>



<a name="199628767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628767">(Jun 03 2020 at 14:47)</a>:</h4>
<p>how/where are <code>Qualif</code>s called?</p>



<a name="199628869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628869">(Jun 03 2020 at 14:48)</a>:</h4>
<p>The file is called validation</p>



<a name="199628959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199628959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199628959">(Jun 03 2020 at 14:49)</a>:</h4>
<p>Im on a phone so linking is a pain XD</p>



<a name="199629098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199629098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199629098">(Jun 03 2020 at 14:50)</a>:</h4>
<p>Adding a new qualif that runs unconditionally is not ideal though, I had hoped there might be a better way</p>



<a name="199629169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199629169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199629169">(Jun 03 2020 at 14:50)</a>:</h4>
<p><span class="user-mention silent" data-user-id="118594">ecstatic-morse</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199628959">said</a>:</p>
<blockquote>
<p>Im on a phone so linking is a pain XD</p>
</blockquote>
<p>np I'll look it up</p>



<a name="199629257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199629257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199629257">(Jun 03 2020 at 14:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="118594">ecstatic-morse</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199628764">said</a>:</p>
<blockquote>
<p>Did you consider my proposal to disallow &amp;mut at the top level of a const or static?</p>
</blockquote>
<p>I'm not <code>Qualif</code>ied enough to say if that works or not but it sounds reasonable to me.</p>



<a name="199629711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199629711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199629711">(Jun 03 2020 at 14:54)</a>:</h4>
<p>what counts as an "item" for <code>ConstCx</code>?</p>
<div class="codehilite"><pre><span></span><code><span class="sd">/// Information about the item currently being const-checked, as well as a reference to the global</span>
<span class="sd">/// context.</span>
</code></pre></div>



<a name="199630753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199630753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199630753">(Jun 03 2020 at 15:02)</a>:</h4>
<p>The syntactical "item" like a fn or a const</p>



<a name="199630818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199630818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199630818">(Jun 03 2020 at 15:02)</a>:</h4>
<p>ok great</p>



<a name="199630887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199630887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199630887">(Jun 03 2020 at 15:03)</a>:</h4>
<p>I'll try to add a new dummy qualif to get familiar with them</p>



<a name="199664768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199664768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199664768">(Jun 03 2020 at 19:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="132916">Christian Poveda</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199624731">said</a>:</p>
<blockquote>
<div class="codehilite"><pre><span></span><code>error: mutable memory (`&amp;mut`) is not allowed in constant
 --&gt; asd.rs:7:1
  |
7 | const BIZ: &amp;&#39;static &amp;&#39;static mut usize = &amp;&amp;mut 5;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
</code></pre></div>


<p>both of you were right.</p>
</blockquote>
<p>is is there any way to know which qualif caused this error?</p>



<a name="199664812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199664812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199664812">(Jun 03 2020 at 19:26)</a>:</h4>
<p>grep for the error message</p>



<a name="199665271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199665271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199665271">(Jun 03 2020 at 19:30)</a>:</h4>
<p>Ok it is done by <code>InternVisitor</code></p>



<a name="199665341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199665341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199665341">(Jun 03 2020 at 19:30)</a>:</h4>
<p>inside <code>visit_value</code></p>



<a name="199665365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199665365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199665365">(Jun 03 2020 at 19:30)</a>:</h4>
<p>but then should I remove that?</p>



<a name="199665571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199665571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199665571">(Jun 03 2020 at 19:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="132916">Christian Poveda</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199665365">said</a>:</p>
<blockquote>
<p>but then should I remove that?</p>
</blockquote>
<p>To accomplish what?</p>



<a name="199665883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199665883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199665883">(Jun 03 2020 at 19:34)</a>:</h4>
<p>I think I don't  understand what's running before what</p>



<a name="199666100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199666100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199666100">(Jun 03 2020 at 19:36)</a>:</h4>
<p>I believe that error originates during const-eval (which runs post-monomorphization) and not during const-checking (which runs before). We want to catch this error in const-checking, which is where the <code>Qualif</code> API lives.</p>



<a name="199666166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199666166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199666166">(Jun 03 2020 at 19:36)</a>:</h4>
<p>ok but then something weird is happening</p>



<a name="199666226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199666226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199666226">(Jun 03 2020 at 19:37)</a>:</h4>
<p>If i remove this: <a href="https://github.com/rust-lang/rust/pull/72934/">https://github.com/rust-lang/rust/pull/72934/</a></p>



<a name="199666272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199666272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199666272">(Jun 03 2020 at 19:37)</a>:</h4>
<div class="codehilite"><pre><span></span><code>error: mutable memory (`&amp;mut`) is not allowed in constant
 --&gt; asd.rs:7:1
  |
7 | const BIZ: &amp;&#39;static &amp;&#39;static mut usize = &amp;&amp;mut 5;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
</code></pre></div>


<p>this error also happens</p>



<a name="199666349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199666349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199666349">(Jun 03 2020 at 19:38)</a>:</h4>
<p>(wrong window)</p>



<a name="199666481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199666481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199666481">(Jun 03 2020 at 19:39)</a>:</h4>
<p>so it seems that "just changing" that line doesn't catch the error</p>



<a name="199666691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199666691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199666691">(Jun 03 2020 at 19:40)</a>:</h4>
<p><span class="user-mention silent" data-user-id="118594">ecstatic-morse</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199666100">said</a>:</p>
<blockquote>
<p>I believe that error originates during const-eval (which runs post-monomorphization) and not during const-checking (which runs before). We want to catch this error in const-checking, which is where the <code>Qualif</code> API lives.</p>
</blockquote>
<p>but then If we manage to write a good enough <code>Qualif</code>  then we should be able to capture cases like these before const-eval (and avoid the <code>InternVisitor</code> error. Right?</p>



<a name="199667160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199667160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199667160">(Jun 03 2020 at 19:43)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> Are we thinking that using dataflow to detect <code>&amp;mut</code> in the return place is the correct approach?</p>



<a name="199667453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199667453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199667453">(Jun 03 2020 at 19:46)</a>:</h4>
<p>I kind of wanted to avoid another dataflow analysis for const-checking. I think my "no <code>&amp;mut</code> at the top-level, only in <code>const fn</code>" rule would accomplish this. Is that too restrictive?</p>



<a name="199668127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199668127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199668127">(Jun 03 2020 at 19:51)</a>:</h4>
<p><span class="user-mention" data-user-id="132916">@Christian Poveda</span> The alternative (which you seem to have discussed in a private forum?) would be to add an <code>AllowsIndirectMutation</code> implementer of the <code>Qualif</code> trait  that is <code>in_any_value_of_ty</code> for all types that contain a mutable reference, and <code>in_adt_inherently</code> for <code>&amp;mut T</code>. Handling <code>&amp;&amp;mut T</code>, which I think could be legal, can be handled later since it will require changing the validation logic in const-eval .</p>



<a name="199668364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199668364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199668364">(Jun 03 2020 at 19:52)</a>:</h4>
<p>You would then check for that <code>Qualif</code> in the return place at all <code>Return</code> terminators in the <code>Visitor</code> in <code>validation.rs</code>, emitting an error if necessary.</p>



<a name="199668651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199668651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199668651">(Jun 03 2020 at 19:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="118594">ecstatic-morse</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199667453">said</a>:</p>
<blockquote>
<p>I kind of wanted to avoid another dataflow analysis for const-checking. I think my "no <code>&amp;mut</code> at the top-level, only in <code>const fn</code>" rule would accomplish this. Is that too restrictive?</p>
</blockquote>
<p>I don't have any strong opinions on this matter. I'd do whatever you think is the correct approach.</p>



<a name="199668654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199668654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199668654">(Jun 03 2020 at 19:55)</a>:</h4>
<p><code>in_adt_inherently</code> is so named because the only things that were a problem in a const context, custom <code>Drop</code> impls and <code>UnsafeCell</code>, were on ADTs, not primitives.</p>



<a name="199668769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199668769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199668769">(Jun 03 2020 at 19:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="132916">Christian Poveda</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199668651">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="118594">ecstatic-morse</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199667453">said</a>:</p>
<blockquote>
<p>I kind of wanted to avoid another dataflow analysis for const-checking. I think my "no <code>&amp;mut</code> at the top-level, only in <code>const fn</code>" rule would accomplish this. Is that too restrictive?</p>
</blockquote>
<p>I don't have any strong opinions on this matter. I'd do whatever you think is the correct approach.</p>
</blockquote>
<p>We need to come to a consensus here still.</p>



<a name="199668877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199668877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199668877">(Jun 03 2020 at 19:57)</a>:</h4>
<p>We meaning <span class="user-group-mention" data-user-group-id="1916">@WG-const-eval</span></p>



<a name="199670794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199670794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199670794">(Jun 03 2020 at 20:11)</a>:</h4>
<p>We'll need to change <code>in_adt_inherently</code> to work on all types. I think it won't quite work as is</p>



<a name="199686475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199686475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199686475">(Jun 03 2020 at 22:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="118594">ecstatic-morse</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199670794">said</a>:</p>
<blockquote>
<p>We'll need to change <code>in_adt_inherently</code> to work on all types. I think it won't quite work as is</p>
</blockquote>
<p>Why? is it because <code>in_adt_inherently</code> is not capable of reading the generic parameters of a type or something like that?</p>



<a name="199712164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199712164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199712164">(Jun 04 2020 at 07:15)</a>:</h4>
<p><span class="user-mention silent" data-user-id="118594">ecstatic-morse</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199667160">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="124288">oli</span> Are we thinking that using dataflow to detect <code>&amp;mut</code> in the return place is the correct approach?</p>
</blockquote>
<p>Not necessarily</p>



<a name="199712245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199712245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199712245">(Jun 04 2020 at 07:16)</a>:</h4>
<p>I think we'd be fine just blocking on <code>&amp;mut</code> in the final type, so we'd forbid <code>Option&lt;&amp;mut T&gt;</code> even if it is <code>None</code></p>



<a name="199712254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199712254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199712254">(Jun 04 2020 at 07:16)</a>:</h4>
<p>which is totally fine, since we can always move to dataflow if we want to</p>



<a name="199772291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199772291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199772291">(Jun 04 2020 at 16:21)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199712245">said</a>:</p>
<blockquote>
<p>I think we'd be fine just blocking on <code>&amp;mut</code> in the final type, so we'd forbid <code>Option&lt;&amp;mut T&gt;</code> even if it is <code>None</code></p>
</blockquote>
<p>That's a breaking change.</p>



<a name="199772768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199772768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199772768">(Jun 04 2020 at 16:24)</a>:</h4>
<p>oh right... then I don't understand how you plan on getting this to work without dataflow</p>



<a name="199773317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199773317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199773317">(Jun 04 2020 at 16:29)</a>:</h4>
<p>If we forbid <code>&amp;mut</code>at the "top-level", there's no way to get a <code>&amp;mut T</code> into the final value of a <code>const</code> without running afoul of the borrow-checker.</p>



<a name="199773513"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199773513" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199773513">(Jun 04 2020 at 16:30)</a>:</h4>
<p>At some point, there will be stuff like <code>Box::leak</code>, but that's a ways off.</p>



<a name="199773545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199773545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199773545">(Jun 04 2020 at 16:30)</a>:</h4>
<p>I get that, I don't get how you want to forbid it. If you aren't forbidding it at the type level (which would be hard due to associated consts anyway), I don't understand where you mean to forbid it</p>



<a name="199773687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199773687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199773687">(Jun 04 2020 at 16:31)</a>:</h4>
<p>The same way we currently forbid <code>&amp;mut</code> in consts, forbid creating them with <code>Rvalue::Ref(BorrowKind::Mut)</code></p>



<a name="199773703"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199773703" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199773703">(Jun 04 2020 at 16:31)</a>:</h4>
<p>or do you mean to forbid any <code>&amp;mut x</code> rvalues inside const contexts except for <code>const fn</code>?</p>



<a name="199773712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199773712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199773712">(Jun 04 2020 at 16:32)</a>:</h4>
<p>ooh</p>



<a name="199773853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199773853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199773853">(Jun 04 2020 at 16:33)</a>:</h4>
<p>we can certainly start this way, as  it's definitely forward compatible to adding it to more places later</p>



<a name="199774386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199774386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199774386">(Jun 04 2020 at 16:37)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199773853">said</a>:</p>
<blockquote>
<p>we can certainly start this way, as adding it to more places is definitely forward compatible</p>
</blockquote>
<p>Yeah, I think once we have heap allocation this will no longer be sufficent, since it's another way to allocate things at compile-time that would live for the duration of the program. Until then, however, I believe the only way to get a long lived thing in const-eval is putting it at the top-level of a <code>const</code> or <code>static</code> or via promotion, which doesn't apply to <code>&amp;mut</code> except for zero-sized arrays.</p>



<a name="199776952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199776952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199776952">(Jun 04 2020 at 16:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/mutable.20borrows.20in.20constants/near/199773703">said</a>:</p>
<blockquote>
<p>or do you mean to forbid any <code>&amp;mut x</code> rvalues inside const contexts except for <code>const fn</code>?</p>
</blockquote>
<p>so, is this the final consensus?</p>



<a name="199777519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199777519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199777519">(Jun 04 2020 at 16:59)</a>:</h4>
<p>I think we're still thinking through the implications. Because we promote <code>&amp;mut []</code>, it would be possible to define a <code>const fn</code> that returns a <code>&amp;'static mut [i32; 0]</code>. Then we could put that into the final value of a <code>const</code>. Is this okay?</p>



<a name="199778734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199778734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199778734">(Jun 04 2020 at 17:08)</a>:</h4>
<p><span class="user-mention" data-user-id="118594">@ecstatic-morse</span> does that mean that if <code>&amp;mut []</code> gets promoted, this:</p>
<blockquote>
<p>or do you mean to forbid any &amp;mut x rvalues inside const contexts except for const fn?</p>
</blockquote>
<p>would cause a compilation error when otherwise it would not have failed?</p>



<a name="199779014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199779014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199779014">(Jun 04 2020 at 17:10)</a>:</h4>
<p>I don't understand the question</p>



<a name="199779036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199779036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199779036">(Jun 04 2020 at 17:10)</a>:</h4>
<p>Maybe an example would help?</p>



<a name="199779253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199779253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199779253">(Jun 04 2020 at 17:12)</a>:</h4>
<p>I don't have a clear example yet. My question is why promoting <code>&amp;mut []</code> is a problem with this approach?</p>



<a name="199779530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199779530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199779530">(Jun 04 2020 at 17:14)</a>:</h4>
<p>It might be incompatible with a future dataflow- or type-based approach. That's not a problem for now, but we should be thinking about what we are ultimately willing to stabilize.</p>



<a name="199839881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199839881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199839881">(Jun 05 2020 at 06:31)</a>:</h4>
<p>Since <code>const fn foo() -&gt; &amp;'static [i32] { &amp;mut [] }</code> doesn't compile on stable today, we can just not allow it in this minimal <code>&amp;mut</code> feature, too</p>



<a name="199839947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199839947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199839947">(Jun 05 2020 at 06:32)</a>:</h4>
<p>Since it even won't compile with the feature gates (<a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=39e910d5ac87ffe533ae2c7f22aa66df">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=39e910d5ac87ffe533ae2c7f22aa66df</a>) I think we're actually fine here</p>



<a name="199984195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199984195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199984195">(Jun 06 2020 at 17:02)</a>:</h4>
<p>Ah, you're right. We only promote <code>&amp;mut []</code> in a <a href="https://github.com/rust-lang/rust/blob/118b50524b79e565f017e08bce9b90a16c63634f/src/librustc_mir/transform/promote_consts.rs#L382-L387">non-const <code>fn</code></a></p>



<a name="199984308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199984308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199984308">(Jun 06 2020 at 17:05)</a>:</h4>
<p>So with that ruled out, I think there's no safe way to get a <code>&amp;'static mut</code> without a top-level <code>&amp;mut</code>.</p>



<a name="199984397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199984397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199984397">(Jun 06 2020 at 17:07)</a>:</h4>
<p>The question now is whether this is worth doing given the usability hit. Users should usually be able to refactor their <code>const</code> or <code>static</code> initializer into a <code>const fn</code> if they need to use a short-lived <code>&amp;mut</code>.  I think we should implement this and see what kind of complaints we get.</p>



<a name="199985730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199985730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199985730">(Jun 06 2020 at 17:43)</a>:</h4>
<p>I agree, this is the best path forward</p>



<a name="199987630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199987630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199987630">(Jun 06 2020 at 18:36)</a>:</h4>
<p><span class="user-mention" data-user-id="132916">@Christian Poveda</span> Okay, we have consensus <span aria-label="smile" class="emoji emoji-263a" role="img" title="smile">:smile:</span>. Do you want to implement this or should I? It should only require a few lines in <code>check_consts::ops</code> (see <code>ops::UnionAccess</code>)</p>



<a name="199987699"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199987699" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199987699">(Jun 06 2020 at 18:39)</a>:</h4>
<p>I want to</p>



<a name="199987700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199987700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199987700">(Jun 06 2020 at 18:39)</a>:</h4>
<p>:D</p>



<a name="199987705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199987705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199987705">(Jun 06 2020 at 18:39)</a>:</h4>
<p>Sweet. It's all yours.</p>



<a name="199987715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/199987715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#199987715">(Jun 06 2020 at 18:39)</a>:</h4>
<p>thanks. I'll take a look and come back with questions.</p>



<a name="200147356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200147356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200147356">(Jun 08 2020 at 19:51)</a>:</h4>
<p><span class="user-mention" data-user-id="118594">@ecstatic-morse</span> <span class="user-mention" data-user-id="124288">@oli</span>  do you think this behavior is correct? This test fails now</p>
<div class="codehilite"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span>: <span class="kt">usize</span>
<span class="p">}</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="mi">0</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">bar</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="n">raw</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">x</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">baz</span><span class="p">(</span><span class="n">foo</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">Foo</span><span class="p">)</span>-&gt; <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="n">raw</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">foo</span><span class="p">.</span><span class="n">x</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="n">_</span>: <span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">foo</span><span class="p">().</span><span class="n">bar</span><span class="p">();</span><span class="w"> </span><span class="c1">// This fails because `Foo::bar`requires a mutable reference.</span>
<span class="w">    </span><span class="n">baz</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">foo</span><span class="p">());</span><span class="w"> </span><span class="c1">// This fails because there is an explicit mutable reference.</span>
<span class="p">};</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>



<a name="200147798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200147798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200147798">(Jun 08 2020 at 19:54)</a>:</h4>
<p>Yes, that seems entirely correct to me</p>



<a name="200147926"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200147926" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200147926">(Jun 08 2020 at 19:55)</a>:</h4>
<p>as in, this test failing to compile is the correct behaviour</p>



<a name="200147945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200147945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200147945">(Jun 08 2020 at 19:55)</a>:</h4>
<p>and especially it failing in the way you describe</p>



<a name="200147971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200147971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200147971">(Jun 08 2020 at 19:56)</a>:</h4>
<p>Yeah to me too. But the first error was a little bit surprising to me from an user's perspective</p>



<a name="200148072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200148072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200148072">(Jun 08 2020 at 19:56)</a>:</h4>
<p>like, I actually had to think about <code>bar</code> requiring the <code>&amp;mut self</code> for a second before realizing why it's an error</p>



<a name="200148106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200148106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200148106">(Jun 08 2020 at 19:56)</a>:</h4>
<p>but i suppose such things aren't in the scope of this PR :P</p>



<a name="200148999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200148999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200148999">(Jun 08 2020 at 20:03)</a>:</h4>
<p>Well :D if you feel up to it, make sure the diagnostic makes it obvious enough</p>



<a name="200149018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200149018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200149018">(Jun 08 2020 at 20:03)</a>:</h4>
<p>if not, open a diagnostics issue ;)</p>



<a name="200149120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200149120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200149120">(Jun 08 2020 at 20:04)</a>:</h4>
<p>I think I'll try to be sure the behavior is correct before complaining/fixing the diagnostics :P</p>



<a name="200149836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200149836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200149836">(Jun 08 2020 at 20:10)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> what should happen with statics?</p>



<a name="200149864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200149864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200149864">(Jun 08 2020 at 20:10)</a>:</h4>
<p>same thing</p>



<a name="200149869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200149869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200149869">(Jun 08 2020 at 20:10)</a>:</h4>
<p>even <code>static mut</code></p>



<a name="200149888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200149888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200149888">(Jun 08 2020 at 20:10)</a>:</h4>
<p>isn't that too restrictive?</p>



<a name="200149891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200149891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200149891">(Jun 08 2020 at 20:10)</a>:</h4>
<p>much simpler to just do everything the same way everywhere</p>



<a name="200149902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200149902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200149902">(Jun 08 2020 at 20:10)</a>:</h4>
<p>less fragile, too</p>



<a name="200150082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200150082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200150082">(Jun 08 2020 at 20:12)</a>:</h4>
<p><code>static mut</code> is considered unsound anyway: <a href="https://github.com/rust-lang/rust/issues/53639">https://github.com/rust-lang/rust/issues/53639</a></p>



<a name="200150114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200150114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200150114">(Jun 08 2020 at 20:12)</a>:</h4>
<p>and <code>static</code> is no different from <code>const</code> wrt mutable references</p>



<a name="200150153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200150153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200150153">(Jun 08 2020 at 20:12)</a>:</h4>
<p>also, we want to start out very restrictive and see if we can get away with it</p>



<a name="200150173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200150173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200150173">(Jun 08 2020 at 20:13)</a>:</h4>
<p>ok then</p>



<a name="200150196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200150196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200150196">(Jun 08 2020 at 20:13)</a>:</h4>
<p>so I don't think there's any <code>too restrictive</code> as long as we get <code>&amp;mut</code> in <code>const fn</code> bodies, args and return types</p>



<a name="200150758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/200150758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#200150758">(Jun 08 2020 at 20:18)</a>:</h4>
<p>Ok then, I updated the tests, if CI is happy with them I think this is ready for review: <a href="https://github.com/rust-lang/rust/pull/72934">https://github.com/rust-lang/rust/pull/72934</a></p>



<a name="205407584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/205407584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#205407584">(Jul 29 2020 at 20:11)</a>:</h4>
<p><span class="user-group-mention" data-user-group-id="1916">@WG-const-eval</span>  I've updated the issues to reflect the status quo and pinged the lang team for stabilization of <code>&amp;mut</code>: <a href="https://github.com/rust-lang/rust/issues/57349#issuecomment-665897390">https://github.com/rust-lang/rust/issues/57349#issuecomment-665897390</a></p>



<a name="205407945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/205407945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#205407945">(Jul 29 2020 at 20:14)</a>:</h4>
<p>great :)</p>



<a name="205408045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/205408045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#205408045">(Jul 29 2020 at 20:15)</a>:</h4>
<p>I still have/want to enable <code>&amp;mut</code> outside <code>const fn</code>s as we discussed but as you said that can be done independently</p>



<a name="205415698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/205415698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#205415698">(Jul 29 2020 at 21:25)</a>:</h4>
<p>Sweet. Thanks <span class="user-mention silent" data-user-id="124288">oli</span>. Const qualification should be sound (although not precise) in the presence of <code>&amp;mut</code>, so I don't know of any blockers.</p>



<a name="205483899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/205483899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#205483899">(Jul 30 2020 at 14:18)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> I have a concern that as we get more and more fractal with our divisions as part of stabilizing what's ready, we create a frustrating experience for users where they are very regularly surprised by implementation limitations on stable</p>



<a name="205483903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/205483903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#205483903">(Jul 30 2020 at 14:18)</a>:</h4>
<p>does that make sense?</p>



<a name="205484718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/205484718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#205484718">(Jul 30 2020 at 14:25)</a>:</h4>
<p>yea</p>



<a name="205486543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/205486543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#205486543">(Jul 30 2020 at 14:38)</a>:</h4>
<p>I'd at least like a better sense of the plan and time frame for 71212</p>



<a name="205487656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/mutable%20borrows%20in%20constants/near/205487656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/mutable.20borrows.20in.20constants.html#205487656">(Jul 30 2020 at 14:47)</a>:</h4>
<p>the reason we did this split is that we didn't have a properly hashed out plan. I'm writing that now by checking our validation logic</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>